home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / utility1 / gs261src.zip / GDEV3B1.C < prev    next >
C/C++ Source or Header  |  1993-05-13  |  22KB  |  799 lines

  1. /* Copyright (C) 1992 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /*
  20.  * gdev3b1.c
  21.  *
  22.  * This is a driver for the AT&T 3b1/7300/UnixPC console display.
  23.  *
  24.  * The image is built in a buffer the size of the page.  Once complete,
  25.  * a screen-sized subset is copied to the screen, and one can scroll
  26.  * through the entire image (move with "vi" or arrow keys).
  27.  *
  28.  * Written by Andy Fyfe, andy@cs.caltech.edu.
  29.  *
  30.  * There are a couple of undesirable "features" that I have found no
  31.  * way to work around.
  32.  *
  33.  * 1) Gs attempts to save the contents of the window before using it, and
  34.  *    then restores the contents afterward.  However, if the gs window is
  35.  *    not the current window, and there are small windows present, then
  36.  *    the saved image is incorrect, and thus the screen will not be correctly
  37.  *    restored.  This seems to be a bug in the 3b1 window driver.  Making
  38.  *    the gs window current before saving its contents is not an acceptable
  39.  *    solution.
  40.  *
  41.  * 2) Gs will enable the scrolling/help/cancel icons if the window has
  42.  *    a border.  Changing these border icons has the side effect of making
  43.  *    the gs window current.  This does circumvent the first problem though.
  44.  */
  45.  
  46. /*
  47.  * About the ATT3B1_PERF flag (notes by Andy Fyfe):
  48.  *
  49.  * I am unable to profile gs on the 3b1, so I added ATT3B1_PERF as a
  50.  * quick way to find out how much time was spent in the 3b1 driver,
  51.  * through dynamically suppressing parts of the code at run time by
  52.  * setting environment variables.  I can then get the time spent in
  53.  * those parts by comparing the results of "time gs ....".
  54.  *
  55.  * At one point this was very useful, and led to a fairly substantial
  56.  * speedup of the fill and copy_mono routines.  It also showed that I
  57.  * wasn't going to get too much more, overall, by further attempts to
  58.  * optimize the 3b1 driver.  So those parts of the code controlled by
  59.  * ATT3B1_PERF have really now outlived their usefulness.
  60.  */
  61.  
  62. #include "gx.h"
  63. #include "gxdevice.h"
  64. #include "gserrors.h"
  65.  
  66. #include <errno.h>
  67. #include <sys/window.h>
  68. #include <sys/termio.h>
  69.  
  70. typedef struct gx_device_att3b1_s {
  71.     gx_device_common;
  72.     int fd;                /* window file descriptor */
  73.     uchar *screen;            /* pointer to screen image */
  74.     ushort line_size;            /* size of screen line in bytes */
  75.     ulong screen_size;            /* size of screen image in bytes */
  76.     int page;                /* page number */
  77. #ifdef ATT3B1_PERF
  78.     char *no_output, *no_fill, *no_copy;
  79. #endif
  80. } gx_device_att3b1;
  81. #define att3b1dev ((gx_device_att3b1 *)dev)
  82.  
  83. #define XDPI    100        /* to get a more-or-less square aspect ratio */
  84. #define YDPI    72
  85. #define XSIZE (8.5 * XDPI)    /* 8.5 x 11 inch page, by default */
  86. #define YSIZE (11 * YDPI)
  87.  
  88. static const ushort masks[] = { 0,
  89.     0x0001, 0x0003, 0x0007, 0x000f,
  90.     0x001f, 0x003f, 0x007f, 0x00ff,
  91.     0x01ff, 0x03ff, 0x07ff, 0x0fff,
  92.     0x1fff, 0x3fff, 0x7fff, 0xffff,
  93. };
  94. static uchar reverse_bits[256] = {
  95.   0, 128, 64, 192, 32, 160, 96, 224, 16, 144, 80, 208, 48, 176, 112, 240,
  96.   8, 136, 72, 200, 40, 168, 104, 232, 24, 152, 88, 216, 56, 184, 120, 248,
  97.   4, 132, 68, 196, 36, 164, 100, 228, 20, 148, 84, 212, 52, 180, 116, 244,
  98.   12, 140, 76, 204, 44, 172, 108, 236, 28, 156, 92, 220, 60, 188, 124, 252,
  99.   2, 130, 66, 194, 34, 162, 98, 226, 18, 146, 82, 210, 50, 178, 114, 242,
  100.   10, 138, 74, 202, 42, 170, 106, 234, 26, 154, 90, 218, 58, 186, 122, 250,
  101.   6, 134, 70, 198, 38, 166, 102, 230, 22, 150, 86, 214, 54, 182, 118, 246,
  102.   14, 142, 78, 206, 46, 174, 110, 238, 30, 158, 94, 222, 62, 190, 126, 254,
  103.   1, 129, 65, 193, 33, 161, 97, 225, 17, 145, 81, 209, 49, 177, 113, 241,
  104.   9, 137, 73, 201, 41, 169, 105, 233, 25, 153, 89, 217, 57, 185, 121, 249,
  105.   5, 133, 69, 197, 37, 165, 101, 229, 21, 149, 85, 213, 53, 181, 117, 245,
  106.   13, 141, 77, 205, 45, 173, 109, 237, 29, 157, 93, 221, 61, 189, 125, 253,
  107.   3, 131, 67, 195, 35, 163, 99, 227, 19, 147, 83, 211, 51, 179, 115, 243,
  108.   11, 139, 75, 203, 43, 171, 107, 235, 27, 155, 91, 219, 59, 187, 123, 251,
  109.   7, 135, 71, 199, 39, 167, 103, 231, 23, 151, 87, 215, 55, 183, 119, 247,
  110.   15, 143, 79, 207, 47, 175, 111, 239, 31, 159, 95, 223, 63, 191, 127, 255
  111. };
  112.  
  113. dev_proc_open_device(att3b1_open);
  114. dev_proc_close_device(att3b1_close);
  115. dev_proc_fill_rectangle(att3b1_fill_rectangle);
  116. dev_proc_copy_mono(att3b1_copy_mono);
  117. dev_proc_output_page(att3b1_output_page);
  118.  
  119. private gx_device_procs att3b1_procs = {
  120.     att3b1_open,
  121.     gx_default_get_initial_matrix,
  122.     gx_default_sync_output,
  123.     att3b1_output_page,
  124.     att3b1_close,
  125.     gx_default_map_rgb_color,
  126.     gx_default_map_color_rgb,
  127.     att3b1_fill_rectangle,
  128.     gx_default_tile_rectangle,
  129.     att3b1_copy_mono,
  130.     gx_default_copy_color,
  131.     gx_default_draw_line,
  132.     gx_default_get_bits,
  133.     gx_default_get_props,
  134.     gx_default_put_props
  135. };
  136.  
  137. gx_device_att3b1 gs_att3b1_device = {
  138.     sizeof(gx_device_att3b1),
  139.     &att3b1_procs,
  140.     "att3b1",
  141.     XSIZE, YSIZE,
  142.     XDPI, YDPI,
  143.     no_margins,
  144.     dci_black_and_white,
  145.     0,
  146.     -1, 0, 0,            /* fd, screen, line_size, */
  147.     0, 0,            /* screen size, page */
  148. #ifdef ATT3B1_PERF
  149.     0, 0, 0,            /* no_output, no_fill, no_copy */
  150. #endif
  151. };
  152.  
  153. int
  154. att3b1_open(gx_device *dev)
  155. {
  156.     struct uwdata uw;
  157.  
  158. #ifdef ATT3B1_PERF
  159.     char *getenv(const char *);
  160. #endif
  161.  
  162.     if (att3b1dev->fd >= 0) {
  163.     close(att3b1dev->fd);
  164.     att3b1dev->fd = -1;
  165.     }
  166.  
  167.     if (att3b1dev->screen != NULL) {
  168.     gs_free((char *)att3b1dev->screen,
  169.         att3b1dev->screen_size, 1, "att3b1_open");
  170.     att3b1dev->screen = 0;
  171.     att3b1dev->screen_size = 0;
  172.     }
  173.  
  174.     att3b1dev->fd = open("/dev/tty", 2);
  175.     if (att3b1dev->fd < 0) {
  176.     lprintf1("att3b1_open: open /dev/tty failed [%d]\n", errno);
  177.     return_error(gs_error_ioerror);
  178.     }
  179.  
  180.     /* Verify that /dev/tty is associated with a console window. */
  181.     if (ioctl(att3b1dev->fd, WIOCGETD, &uw) < 0) {
  182.     lprintf1("att3b1_open: can not obtain window data [%d]\n", errno);
  183.     lprintf("att3b1_open: the att3b1 device requires a console window\n");
  184.     att3b1_close(dev);
  185.     return_error(gs_error_ioerror);
  186.     }
  187.  
  188.     /* we need an even number of bytes per line */
  189.     att3b1dev->line_size = ((att3b1dev->width + 15) / 16) * 2;
  190.     att3b1dev->screen_size = att3b1dev->line_size * att3b1dev->height;
  191.  
  192.     att3b1dev->screen =
  193.     (uchar *)gs_malloc(att3b1dev->screen_size, 1, "att3b1_open");
  194.     if (att3b1dev->screen == NULL) {
  195.     att3b1_close(dev);
  196.     return_error(gs_error_VMerror);
  197.     }
  198.  
  199.     att3b1dev->page = 1;
  200.  
  201. #ifdef ATT3B1_PERF
  202.     att3b1dev->no_output = getenv("GS_NOOUTPUT");
  203.     att3b1dev->no_fill = getenv("GS_NOFILL");
  204.     att3b1dev->no_copy = getenv("GS_NOCOPY");
  205. #endif
  206.  
  207.     return 0;
  208. }
  209.  
  210. int
  211. att3b1_close(gx_device *dev)
  212. {
  213.     if (att3b1dev->fd >= 0) {
  214.     close(att3b1dev->fd);
  215.     att3b1dev->fd = -1;
  216.     }
  217.  
  218.     if (att3b1dev->screen != NULL) {
  219.     gs_free((char *)att3b1dev->screen,
  220.         att3b1dev->screen_size, 1, "att3b1_close");
  221.     att3b1dev->screen = 0;
  222.     att3b1dev->screen_size = 0;
  223.     }
  224.  
  225.     return 0;
  226. }
  227.  
  228. int
  229. att3b1_fill_rectangle(gx_device *dev, int x, int y, int w, int h,
  230.                       gx_color_index colour)
  231. {
  232.     uint o, b, wl, wr, w2;
  233.     ushort *p, *q, maskl, maskr;
  234.  
  235. #ifdef ATT3B1_PERF
  236.     if (att3b1dev->no_fill) return 0;
  237. #endif
  238.  
  239.     fit_fill(dev, x, y, w, h);
  240.  
  241.     /* following fit_fill, we can assume x, y, w, h are unsigned. */
  242.  
  243.     p = (ushort *)&att3b1dev->screen[(ushort)y*att3b1dev->line_size] +
  244.     (uint)x/16;
  245.     o = (uint)x % 16;
  246.     b = 16 - o;
  247.     wl = ((uint)w < b) ? (uint)w : b;
  248.     maskl = masks[wl] << o;
  249.     w -= wl;
  250.     wr = (uint)w % 16;
  251.     maskr = masks[wr];
  252.  
  253.     if (colour == 0) {
  254.     maskl = ~maskl;
  255.     maskr = ~maskr;
  256.     while (h-- > 0) {
  257.         q = p;
  258.         w2 = w;
  259.         *q++ &= maskl;
  260.         while (w2 >= 16) {
  261.         *q++ = 0;
  262.         w2 -= 16;
  263.         }
  264.         *q &= maskr;
  265.         p += (att3b1dev->line_size / 2);
  266.     }
  267.     }
  268.     else {
  269.     while (h-- > 0) {
  270.         q = p;
  271.         w2 = w;
  272.         *q++ |= maskl;
  273.         while (w2 >= 16) {
  274.         *q++ = 0xffff;
  275.         w2 -= 16;
  276.         }
  277.         *q |= maskr;
  278.         p += (att3b1dev->line_size / 2);
  279.     }
  280.     }
  281.  
  282.     return 0;
  283. }
  284.  
  285. #ifdef __GNUC__
  286. #define rotate(value, count) \
  287.     asm("ror%.l    %2,%0" : "=d" (value) : "0" (value), "d" (count))
  288. #else
  289. #define rotate(value, count) \
  290.     value = (value >> count) | (value << (32-count))
  291. #endif
  292.  
  293. int
  294. att3b1_copy_mono(gx_device *dev, const uchar *data,
  295.          int data_x, int raster, gx_bitmap_id id,
  296.          int x, int y, int width, int height, 
  297.          gx_color_index colour0, gx_color_index colour1)
  298. {
  299.     const ushort *src_p, *src_q;
  300.     ushort *dst_p, *dst_q;
  301.     ulong bits, mask, *p;
  302.     uint src_o, src_b, dst_o, dst_b, op;
  303.     uint w1, w2;
  304.  
  305. #ifdef ATT3B1_PERF
  306.     if (att3b1dev->no_copy) return 0;
  307. #endif
  308.  
  309.     if (colour1 == colour0)        /* vacuous case */
  310.     return att3b1_fill_rectangle(dev, x, y, width, height, colour0);
  311.  
  312.     fit_copy(dev, data, data_x, raster, id, x, y, width, height);
  313.  
  314.     /* following fit_copy, we can assume x, y, width, height are unsigned. */
  315.  
  316.     /*
  317.      * In what follows, we're assuming that each row of the input bitmap
  318.      * is short-aligned, that is, that both "data" and "raster" are even.
  319.      */
  320.     src_p = ((const ushort *)data) + (uint)data_x/16;
  321.     src_o = (uint)data_x % 16;
  322.     src_b = 16 - src_o;
  323.  
  324.     dst_p = (ushort *)&att3b1dev->screen[(ushort)y*att3b1dev->line_size] +
  325.         (uint)x/16;
  326.     dst_o = (uint)x % 16;
  327.     dst_b = 16 - dst_o;
  328.  
  329.     op = (int)colour0 * 3 + (int)colour1 + 4;
  330.  
  331.     while (height-- > 0) {
  332.     w2 = width;
  333.     src_q = src_p;
  334.     dst_q = dst_p;
  335.  
  336.     while (w2 > 0) {
  337.         w1 = (w2 < 16) ? w2 : 16;
  338.         mask = masks[w1];
  339.         /*
  340.          * We are assuming that the bitmap "data" is typically aligned.
  341.          * Thus the test for this special case is typically a win over
  342.          * a 16-bit shift.
  343.          */
  344.         if (src_o == 0)
  345.         bits = *src_q++;
  346.         else {
  347.         bits = *((ulong *)src_q) >> src_b;
  348.         bits &= 0xffff;
  349.         src_q++;
  350.         }
  351.         if (w1 <= 8)
  352.         bits = reverse_bits[bits>>8];
  353.         else
  354.         bits = (reverse_bits[bits&0xff] << 8) | reverse_bits[bits>>8];
  355.         /*
  356.          * While the input bit map is assumed to be typically aligned, we
  357.          * assume that the place in the image is not.  Thus we don't
  358.          * separate out the aligned case.  Doing so would cost a test,
  359.          * and only reduce the average shift by about 1.
  360.          */
  361.         p = (ulong *)dst_q;
  362.         switch(op) {
  363.         case 1:    /* not src and dst */
  364.         bits = ~(bits & mask);
  365.         rotate(bits,dst_b);
  366.         *p &= bits;
  367.         break;
  368.         case 2:    /* src or dst */
  369.         bits = bits & mask;
  370.         rotate(bits,dst_b);
  371.         *p |= bits;
  372.         break;
  373.         case 3:    /* src and dst */
  374.         bits = bits | ~mask;
  375.         rotate(bits,dst_b);
  376.         *p &= bits;
  377.         break;
  378.         case 5:    /* src */
  379.         rotate(bits,dst_b);
  380.         rotate(mask,dst_b);
  381.         *p = (*p & ~mask) | (bits & mask);
  382.         break;
  383.         case 6:    /* not src or dst */
  384.         bits = ~bits & mask;
  385.         rotate(bits,dst_b);
  386.         *p |= bits;
  387.         break;
  388.         case 7:    /* not src */
  389.         rotate(bits,dst_b);
  390.         rotate(mask,dst_b);
  391.         *p = (*p & ~mask) | (~bits & mask);
  392.         break;
  393.         }
  394.         dst_q++;
  395.         w2 -= w1;
  396.     }
  397.  
  398.     src_p += (raster / 2);
  399.     dst_p += (att3b1dev->line_size / 2);
  400.     }
  401.  
  402.     return 0;
  403. }
  404.  
  405. static int getKeyboard(gx_device *);
  406.  
  407. const char *help_msg[] = {
  408.     "h, j, k, l, UP, DOWN, LEFT, RIGHT  move the page (0.25\" h, 0.5\" v)",
  409.     "H, J, K, L, BEG, END               move to far edge of the page",
  410.     "^U, ^D, ROLL UP, ROLL DOWN         scroll up or down (1/2 screen height)",
  411.     "^F, ^B, PAGE UP, PAGE DOWN         scroll up or down (full screen height)",
  412.     "c, C                               centre page horizontally, vertically",
  413.     "<, >, ^, _                         fine movements (single pixel)",
  414.     "^L, ^R, r, HOME                    move to default position",
  415.     "=, MARK                            make current position the default",
  416.     "I                                  invert the image (black <-> white)",
  417.     "q, x, ^C, EXIT, CANCL, n, f, NEXT,",
  418.     "    SPACE, RETURN, ENTER           end the page",
  419.     "?, HELP                            help screen",
  420. };
  421.  
  422. static void
  423. do_help(gx_device *dev)
  424. {
  425.     int i;
  426.     struct utdata ut;
  427.  
  428.     /* we would like to save the cursor position, but we can't */
  429.     write(att3b1dev->fd, "\033[2J\033[H", 7);
  430.  
  431.     /* write help screen */
  432.     for (i=0; i < sizeof(help_msg)/sizeof(help_msg[0]); ++i) {
  433.     write(att3b1dev->fd, help_msg[i], strlen(help_msg[i]));
  434.     write(att3b1dev->fd, "\n", 1);
  435.     }
  436.     ut.ut_num = WTXTSLK1;
  437.     strcpy(ut.ut_text, "Press any key to continue");
  438.     ioctl(att3b1dev->fd, WIOCSETTEXT, &ut);
  439.  
  440.     /* wait for keyboard input */
  441.     i = getKeyboard(dev);
  442.  
  443.     /* clear screen and put cursor at the bottom of the screen */
  444.     write(att3b1dev->fd, "\033[2J\033[99;1H", 11);
  445. }
  446.  
  447. int
  448. att3b1_output_page(gx_device *dev, int num_copies, int flush)
  449. {
  450.     struct urdata ur;
  451.     struct utdata ut, ut_orig;
  452.     struct uwdata uw;
  453.     int uflags;
  454.     struct termio old, new;
  455.     int xorigin, yorigin;
  456.     static int def_xorigin = 0, def_yorigin = 0;
  457.     int screen_width, screen_height;
  458.     int inverted = 0;
  459.     int error = 0;
  460.     int ch;
  461.     ushort *p;
  462.     ushort save_image[WINWIDTH * WINHEIGHT / 16];
  463.  
  464. #ifdef ATT3B1_PERF
  465.     if (att3b1dev->no_output) return 0;
  466. #endif
  467.  
  468.     /*
  469.      * initialize, and save screen state
  470.      */
  471.  
  472.     if (ioctl(att3b1dev->fd, WIOCGETD, &uw) < 0) {
  473.     lprintf1("att3b1_output_page: window WIOCGETD ioctl failed [%d]\n",
  474.         errno);
  475.     att3b1_close(dev);
  476.     return_error(gs_error_ioerror);
  477.     }
  478.  
  479.     /*
  480.      * we assume, henceforth, that screen ioctl calls will succeed
  481.      */
  482.  
  483.     write(att3b1dev->fd, "\a\033[=1C", 6);
  484.  
  485.     uflags = uw.uw_uflags;
  486.     if (!(uflags & NBORDER)) {
  487.     uw.uw_uflags = BORDHSCROLL | BORDVSCROLL | BORDHELP | BORDCANCEL;
  488.     ioctl(att3b1dev->fd, WIOCSETD, &uw);
  489.     }
  490.  
  491.     ut_orig.ut_num = WTXTSLK1;
  492.     ioctl(att3b1dev->fd, WIOCGETTEXT, &ut_orig);
  493.  
  494.     /* This isn't necessary, but helps a bit when the following attempt
  495.        to get the current screen image fails (without any indication). */
  496.     memset(save_image, '\0', sizeof(save_image));
  497.  
  498.     ur.ur_srcbase = 0;
  499.     ur.ur_srcwidth = 0;
  500.     ur.ur_srcx = 0;
  501.     ur.ur_srcy = 0;
  502.     ur.ur_dstbase = save_image;
  503.     ur.ur_dstwidth = WINWIDTH / 8;
  504.     ur.ur_dstx = 0;
  505.     ur.ur_dsty = 0;
  506.     ur.ur_width = uw.uw_width;
  507.     ur.ur_height = uw.uw_height;
  508.     ur.ur_srcop = SRCSRC;
  509.     ur.ur_dstop = DSTSRC;
  510.     ur.ur_pattern = 0;
  511.     ioctl(att3b1dev->fd, WIOCRASTOP, &ur);
  512.  
  513.     ioctl(att3b1dev->fd, TCGETA, &old);
  514.     new = old;
  515.     new.c_lflag &= ~(ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHONL);
  516.     new.c_cc[VMIN] = 1;
  517.     ioctl(att3b1dev->fd, TCSETAF, &new);
  518.  
  519.     screen_width = (uw.uw_width < att3b1dev->width) ? uw.uw_width
  520.                 : att3b1dev->width;
  521.     screen_height = (uw.uw_height < att3b1dev->height) ? uw.uw_height
  522.                 : att3b1dev->height;
  523.  
  524.     write(att3b1dev->fd, "\033[2J", 4);
  525.  
  526.     ur.ur_srcwidth = att3b1dev->line_size;
  527.     ur.ur_width = screen_width;
  528.     ur.ur_height = screen_height;
  529.     ur.ur_dstbase = 0;
  530.     ur.ur_dstwidth = 0;
  531.  
  532.     /*
  533.      * allow one to move the screen window through the entire image
  534.      */
  535.  
  536.     xorigin = def_xorigin;
  537.     yorigin = def_yorigin;
  538.  
  539.     while (1) {
  540.     /* Things go bad if ur_srcx >= 2048 */
  541.     ur.ur_srcbase = (ushort *)att3b1dev->screen + (xorigin >> 4);
  542.     ur.ur_srcx = xorigin & 15;
  543.     ur.ur_srcy = yorigin;
  544.  
  545.     if (ioctl(att3b1dev->fd, WIOCRASTOP, &ur) < 0) {
  546.         lprintf1(
  547.         "att3b1_output_page: window WIOCRASTOP ioctl failed [%d]\n",
  548.         errno);
  549.         error = gs_error_ioerror;
  550.     }
  551.  
  552.     ut.ut_num = WTXTSLK1;
  553.     sprintf(ut.ut_text,
  554.         "%s %d, top right (%d,%d), size (%d,%d), press '?' for help.",
  555.         flush ? "Showpage" : "Copypage", att3b1dev->page, xorigin, yorigin,
  556.         att3b1dev->width, att3b1dev->height);
  557.     ioctl(att3b1dev->fd, WIOCSETTEXT, &ut);
  558.  
  559.     ch = error ? 'q' : getKeyboard(dev);
  560.  
  561.     switch(ch) {
  562.     case 'h':
  563.         xorigin -= ((uint)(int)att3b1dev->x_pixels_per_inch+3)/4;
  564.         break;
  565.  
  566.     case 'k':
  567.         yorigin -= ((uint)(int)att3b1dev->y_pixels_per_inch+1)/2;
  568.         break;
  569.  
  570.     case 'l':
  571.         xorigin += ((uint)(int)att3b1dev->x_pixels_per_inch+3)/4;
  572.         break;
  573.  
  574.     case 'j':
  575.         yorigin += ((uint)(int)att3b1dev->y_pixels_per_inch+1)/2;
  576.         break;
  577.  
  578.     case 'H':
  579.         xorigin = 0;
  580.         break;
  581.  
  582.     case 'K':
  583.         yorigin = 0;
  584.         break;
  585.  
  586.     case 'L':
  587.         xorigin = att3b1dev->width - screen_width;
  588.         break;
  589.  
  590.     case 'J':
  591.         yorigin = att3b1dev->height - screen_height;
  592.         break;
  593.  
  594.     case '<':
  595.         xorigin -= 1;
  596.         break;
  597.  
  598.     case '>':
  599.         xorigin += 1;
  600.         break;
  601.  
  602.     case '^':
  603.         yorigin -= 1;
  604.         break;
  605.  
  606.     case '_':
  607.         yorigin += 1;
  608.         break;
  609.  
  610.         
  611.     case '\025':    /* control-U */
  612.         yorigin -= screen_height/2;
  613.         break;
  614.  
  615.     case '\004':    /* control-D */
  616.         yorigin += screen_height/2;
  617.         break;
  618.  
  619.     case '\002':    /* control-B */
  620.         yorigin -= screen_height;
  621.         break;
  622.  
  623.     case '\006':    /* control-F */
  624.         yorigin += screen_height;
  625.         break;
  626.  
  627.     case '\f':
  628.     case 'r' :
  629.     case '\022':    /* control-R */
  630.         xorigin = def_xorigin;
  631.         yorigin = def_yorigin;
  632.         break;
  633.     
  634.     case 'c':    /* centre horizontally */
  635.         xorigin = (att3b1dev->width - screen_width) / 2;
  636.         break;
  637.     
  638.     case 'C':    /* centre vertically */
  639.         yorigin = (att3b1dev->height - screen_height) / 2;
  640.         break;
  641.  
  642.     case '=':
  643.         def_xorigin = xorigin;
  644.         def_yorigin = yorigin;
  645.         break;
  646.  
  647.     case 'I':
  648.         for (p = (ushort *)att3b1dev->screen;
  649.           p < (ushort *)&att3b1dev->screen[att3b1dev->screen_size]; ++p)
  650.         *p = ~ *p;
  651.         inverted = !inverted;
  652.         break;
  653.     
  654.     case '?':
  655.         do_help(dev);
  656.         break;
  657.     
  658.     case -1:
  659.         error = gs_error_ioerror;
  660.         /* fall through, for cleanup */
  661.  
  662.     case 'q':
  663.     case 'x':
  664.     case '\003':    /* control-C */
  665.     case 'n':
  666.     case 'f':
  667.     case ' ':
  668.     case '\n':
  669.     case '\r':
  670.         if (flush)
  671.         att3b1dev->page++;
  672.         else if (inverted)    /* restore inverted image for copypage */
  673.         for (p = (ushort *)att3b1dev->screen;
  674.           p < (ushort *)&att3b1dev->screen[att3b1dev->screen_size]; ++p)
  675.             *p = ~ *p;
  676.         if (!(uflags & NBORDER)) {
  677.         ioctl(att3b1dev->fd, WIOCGETD, &uw); /*window may have moved*/
  678.         uw.uw_uflags = uflags;
  679.         ioctl(att3b1dev->fd, WIOCSETD, &uw);
  680.         }
  681.         ur.ur_srcbase = save_image;
  682.         ur.ur_srcwidth = WINWIDTH / 8;
  683.         ur.ur_width = uw.uw_width;
  684.         ur.ur_height = uw.uw_height;
  685.         ur.ur_srcx = 0;
  686.         ur.ur_srcy = 0;
  687.         ioctl(att3b1dev->fd, WIOCRASTOP, &ur);
  688.         ioctl(att3b1dev->fd, WIOCSETTEXT, &ut_orig);
  689.         ioctl(att3b1dev->fd, TCSETAF, &old);
  690.         write(att3b1dev->fd, "\033[=0C", 5);
  691.  
  692.         if (error) {
  693.         att3b1_close(dev);
  694.         return_error(error);
  695.         }
  696.         else
  697.         return 0;
  698.     }
  699.  
  700.     if (xorigin >= att3b1dev->width - screen_width)
  701.         xorigin = att3b1dev->width - screen_width;
  702.     if (xorigin < 0)
  703.         xorigin = 0;
  704.     if (yorigin >= att3b1dev->height - screen_height)
  705.         yorigin = att3b1dev->height - screen_height;
  706.     if (yorigin < 0)
  707.         yorigin = 0;
  708.     }
  709. }
  710.  
  711. static int
  712. get_char(gx_device *dev)
  713. {
  714.     char ch;
  715.     int count;
  716.  
  717.     count = read(att3b1dev->fd, &ch, 1);
  718.     if (count == 0)
  719.     return 'q';
  720.     else if (count < 0)
  721.     return -1;
  722.     else
  723.     return ch;
  724. }
  725.  
  726. static int
  727. getKeyboard(gx_device *dev)
  728. {
  729.     char ch;
  730.  
  731.     ch = get_char(dev);
  732.  
  733.     if (ch != '\033')
  734.     return ch;
  735.  
  736.     /*
  737.      * If the char is escape, interpret the escape sequence and return
  738.      * an equivalent single character.
  739.      *
  740.      * Note that a mouse click on a window border icon is translated
  741.      * to the corresponding key, for example, the "up" icon generates
  742.      * roll-up/page-up/beg for the left/middle/right mouse button.
  743.      */
  744.  
  745.     switch (get_char(dev)) {
  746.     case '[':
  747.     switch(get_char(dev)) {
  748.     case 'A':    /* up arrow */
  749.         return 'k';
  750.     case 'T':    /* shift up arrow (roll up) */
  751.         return '\025';
  752.     case 'B':    /* down arrow */
  753.         return 'j';
  754.     case 'S':    /* shift down arrow (roll down) */
  755.         return '\004';
  756.     case 'C':    /* right arrow */
  757.         return 'l';
  758.     case 'D':    /* left arrow */
  759.         return 'h';
  760.     case 'H':    /* home */
  761.         return 'r';
  762.     case 'U':    /* page down */
  763.         return '\006';
  764.     case 'V':    /* page up */
  765.         return '\002';
  766.     }
  767.     break;
  768.     case 'O':
  769.     switch(get_char(dev)) {
  770.     case 'm':    /* help */
  771.     case 'M':    /* shift help */
  772.         return '?';
  773.     case 'k':    /* exit */
  774.     case 'K':    /* shift exit */
  775.     case 'w':    /* cancl */
  776.     case 'W':    /* shift cancl */
  777.         return 'q';
  778.     }
  779.     break;
  780.     case 'N':
  781.     switch(get_char(dev)) {
  782.     case 'h':    /* next */
  783.         return 'f';
  784.     case 'i':    /* mark */
  785.         return '=';
  786.     case 'L':    /* shift right arrow */
  787.         return 'l';
  788.     case 'K':    /* shift left arrow */
  789.         return 'h';
  790.     }
  791.     break;
  792.     case '9':    /* Beg */
  793.     return 'K';
  794.     case '0':    /* End */
  795.     return 'J';
  796.     }
  797.     return '\0';
  798. }
  799.